﻿using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Xml;
using Umbraco.Forms.Data.Storage;

namespace Cultiv.RestContour
{
    public class XsltExtensions
    {
        public static XmlNode GetField(string fieldId)
        {
            var formGuid = GetContourGuid();
            var xmlDoc = new XmlDocument();

            using (var stringReader = new StringReader(Helper.GetField(formGuid, fieldId, "xml").Trim()))
                xmlDoc.Load(stringReader);

            var node = xmlDoc.DocumentElement;

            return node;
        }
        public static XmlNode GetFieldByFieldName(string fieldName)
        {
            var formGuid = GetContourGuid();
            var xmlDoc = new XmlDocument();

            using (var stringReader = new StringReader(Helper.GetFieldByFieldName(formGuid, fieldName, "xml").Trim()))
                xmlDoc.Load(stringReader);

            var node = xmlDoc.DocumentElement;

            return node;
        }

        public static XmlNode GetPrevaluesForField(string fieldId)
        {
            var formGuid = GetContourGuid();
            var xmlDoc = new XmlDocument();

            using (var stringReader = new StringReader(Helper.GetPrevaluesForField(formGuid, fieldId, "xml").Trim()))
                xmlDoc.Load(stringReader);

            var node = xmlDoc.DocumentElement;

            return node;
        }

        public static XmlNode GetPrevaluesForFieldName(string fieldName)
        {
            var formGuid = GetContourGuid();
            var xmlDoc = new XmlDocument();

            using (var stringReader = new StringReader(Helper.GetPrevaluesForFieldName(formGuid, fieldName, "xml").Trim()))
                xmlDoc.Load(stringReader);

            var node = xmlDoc.DocumentElement;

            return node;
        }

        public static XmlNode GetFormFields()
        {
            var formGuid = GetContourGuid();
            var xmlDoc = new XmlDocument();

            using (var stringReader = new StringReader(Helper.GetFormFields(formGuid, "xml").Trim()))
                xmlDoc.Load(stringReader);

            var node = xmlDoc.DocumentElement;

            return node;
        }

        public static string GetValidationRules()
        {
            var formGuid = GetContourGuid();
            var rules = "\r\nvar rules = {\r\n";

            using (var formStorage = new FormStorage())
            {
                var currentForm = formStorage.GetForm(new Guid(formGuid));

                for (var i = 0; i < currentForm.AllFields.Count; i++)
                {
                    var field = currentForm.AllFields[i];

                    if (i != 0)
                        rules += ",\r\n";

                    rules += "\"" + field.Caption + "\":" + " { required: " + field.Mandatory.ToString().ToLower();
                    if (field.RegEx != "")
                        rules += ", regex: " + "/" + HttpContext.Current.Server.HtmlDecode(field.RegEx) + "/";

                    rules += " }";
                }
            }

            rules += "\r\n};";

            var messages = "\r\nvar messages = {\r\n";
            using (var formStorage = new FormStorage())
            {
                var currentForm = formStorage.GetForm(new Guid(formGuid));

                for (var i = 0; i < currentForm.AllFields.Count; i++)
                {
                    var field = currentForm.AllFields[i];

                    if (i != 0)
                        messages += ",\r\n";

                    var requiredMessage = field.RequiredErrorMessage == "" ? currentForm.RequiredErrorMessage : field.RequiredErrorMessage;
                    var invalidMessage = field.InvalidErrorMessage == "" ? currentForm.InvalidErrorMessage : field.InvalidErrorMessage;

                    messages += "\"" + field.Caption + "\":" + " { required: " + "\"" + string.Format(requiredMessage, field.Caption) + "\"";

                    if (field.RegEx != "")
                    {
                        messages += ", ";
                        messages += " regex: " + "\"" + string.Format(invalidMessage, field.Caption) + "\"";
                    }
                    messages += " }";
                }
            }
            messages += "\r\n};";

            return rules + messages;
        }

        public static string BuildForm()
        {
            var formGuid = GetContourGuid();
            return GetValidationRules() + "\r\n\r\nbuildFormValidator(\"" + formGuid + "\", rules, messages);\r\n";
        }

        public static void SetContourGuid(string formGuid)
        {
            if (((Page)HttpContext.Current.Handler).Items.Contains("formGuid") == false)
                ((Page)HttpContext.Current.Handler).Items.Add("formGuid", formGuid);
            else
                ((Page)HttpContext.Current.Handler).Items["formGuid"] = formGuid;
        }

        public static string GetContourGuid()
        {
            return ((Page)HttpContext.Current.Handler).Items.Contains("formGuid") ? ((Page)HttpContext.Current.Handler).Items["formGuid"].ToString() : "";
        }
    }
}